//
//  ZZCComposeToolBar.swift
//  weibo
//
//  Created by zhangzuchao on 2017/4/5.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit

enum ZZCComposeToolBarButtonType: Int {
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  #
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
}

class ZZCComposeToolBar: UIStackView {

    //  点击toolbar按钮闭包的回调
    var buttonActionClosure: ((ZZCComposeToolBarButtonType)->())?
    
    //表情按钮
      var emoticonButton: UIButton?
    
    //重写init方法 控制器直接调用该方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //  水平方向布局(默认水平方向)
        self.axis = .horizontal
        //  子控件等比填充
        self.distribution = .fillEqually
        
        addChildButton(imageName: "compose_camerabutton_background", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }

    //  添加子按钮
     @discardableResult //   表示可以忽略结果
    fileprivate func addChildButton(imageName: String, type: ZZCComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
 
        //  枚举的原始值作为tag使用表示(0,1,2,3,4就是你设置原始值)
        button.tag = type.rawValue
        
        //  添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn: )), for: .touchUpInside)
        
        //  设置不同状态下图片
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        
        //  取消高亮
        button.adjustsImageWhenHighlighted = false
        
        //  添加按钮
        addArrangedSubview(button)
         return button
    }

    //  MARK: ---   点击事件
    @objc private func buttonAction(btn: UIButton) {
        
        //  通过枚举的原始值获取枚举
        let buttonType = ZZCComposeToolBarButtonType(rawValue: btn.tag)!
        
        //  点击按钮闭包的回调
        buttonActionClosure?(buttonType)
    }

    //  根据是否是表情键盘类型切换对应的icon
    func switchEmoticonIcon(isEmoticon: Bool) {
        if isEmoticon {
            //  表情键盘-> 改成系统的icon
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        } else {
            //  系统键盘-> 改成表情的icon
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
    

}
